/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.DccrDAO;
import com.cashmate.hibernate.pojo.Dccr;
import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

/**
 *
 * @author Dean
 */
public class EditDccr extends ActionSupport {

    public static Logger logger = Logger.getLogger(EditDccr.class);
    private String id;
    private String debitedpayment;
    private final DccrDAO dccrdao = (DccrDAO) ServletActionContext.getServletContext().getAttribute("dccrDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");

    @Action(value = "/edit-dccr")
    @Override
    public String execute() throws Exception {
        try {
            if (debitedpayment != null) {
                double d = Double.parseDouble(debitedpayment);
                int dccrid = Integer.parseInt(id);
                Dccr dccr = dccrdao.get(dccrid);
                dccr.setDebitedpayment(d);
                dccrdao.update(dccr);
                dccrdao.updateDccr(dccr.getDategenerated(), dccr.getBranchid());
               /* 
                List<Dccr> debitList = (List<Dccr>) ServletActionContext.getRequest().getSession().getAttribute("debitList");
                if(debitList==null){
                    debitList = new LinkedList<Dccr>();
                }
                if(debitList.contains(dccr)){
                    debitList.remove(dccr);
                }
                debitList.add(dccr);
                ServletActionContext.getRequest().getSession().setAttribute("debitList", debitList);       
               */
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        } finally {
            DccrDAO.close();
        }
        return NONE;
    }

    public void setDebitedpayment(String debitedpayment) {
        this.debitedpayment = debitedpayment;
    }

    public void setId(String id) {
        this.id = id;
    }
}
